package com.china08.yxyapi.api.manage.sch.platform.statistics.resource;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.statistics.SchoolUploadResourceDay;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.statistics.SchoolUploadResDayRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公 资源统计学校 资源统计 趋势变化
 * 
 */
@CrossOrigin
@RestController
public class ResourceAllTrendChangeController extends YxyApiBaseController {
	@Autowired
	private SchoolUploadResDayRepository schoolUploadResDayRepository;

	@ApiOperation(value = "资源统计学校    资源统计    趋势变化")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/sch/platform/statistics/resource/trendchange/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req) {
		RespModel model = new RespModel();
		List<SchoolUploadResourceDay> areaResourceReportLst = new ArrayList<SchoolUploadResourceDay>();

		// 资源上传数
		List<Long> rescountLst = new ArrayList<Long>();
		// 微课上传数量
		List<Long> weikeCountLst = new ArrayList<Long>();
		
		//一周的年月日
		List<String> weekList=new ArrayList<String>();
		//一个月的年月日
		List<String> monthList=new ArrayList<String>();
		if (req.dateValue.equals("一周")) {
			String schoolId = getProfile(request, response).getSchoolId();
			Long resCount = 0L;
			Long weiKeCount = 0L;
			ArrayList<Integer> oldDate = getOldDate(7);
			for (Integer integer : oldDate) {
				String dateStr=String.valueOf(integer);
				String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
				weekList.add(strDate);
			}
			for (Integer oldDates : oldDate) {
				areaResourceReportLst = schoolUploadResDayRepository.findBySchoolIdAndIntDate(schoolId, oldDates);
				if (areaResourceReportLst.size() > 0) {
					for (SchoolUploadResourceDay areaResourceReport : areaResourceReportLst) {
						resCount += areaResourceReport.getResCount();
						weiKeCount += areaResourceReport.getWeikeCount();
					}
				} else {
					resCount = 0L;
					weiKeCount = 0L;
				}

				rescountLst.add(resCount);
				weikeCountLst.add(weiKeCount);
				resCount = 0L;
				weiKeCount = 0L;
			}
		}
		if (req.dateValue.equals("一个月")) {
			String schoolId = getProfile(request, response).getSchoolId();
			Long resCount = 0L;
			Long weiKeCount = 0L;
			ArrayList<Integer> oldDate = getOldDate(30);
			for (Integer integer : oldDate) {
				String dateStr=String.valueOf(integer);
				String strDate=dateStr.substring(0, 4)+"-"+dateStr.substring(4, 6)+"-"+dateStr.substring(6, 8);
				monthList.add(strDate);
			}
			for (Integer oldDates : oldDate) {
				areaResourceReportLst = schoolUploadResDayRepository.findBySchoolIdAndIntDate(schoolId, oldDates);
				if (areaResourceReportLst.size() > 0) {
					for (SchoolUploadResourceDay areaResourceReport : areaResourceReportLst) {
						resCount += areaResourceReport.getResCount();
						weiKeCount += areaResourceReport.getWeikeCount();
					}
				} else {
					resCount = 0L;
					weiKeCount = 0L;
				}

				rescountLst.add(resCount);
				weikeCountLst.add(weiKeCount);
				resCount = 0L;
				weiKeCount = 0L;
			}
		}

		model.rescountLst = rescountLst;
		model.weikeCountLst = weikeCountLst;
		model.weekList=weekList;
		model.monthList=monthList;
		return model;
	}

	protected static class RespModel extends YxyApiRespModel {
		// 资源上传数
		public List<Long> rescountLst;
		// 微课上传数量
		public List<Long> weikeCountLst;
		// 一周的年月日
		public List<String> weekList;
		// 一个月的年月日
		public List<String> monthList;
	}

	protected static class ReqModel extends YxyApiReqModel {
		// 一周或者是一个月
		public String dateValue;
	}

	// 获取过去N天的时间
	public static ArrayList<Integer> getOldDate(int intervals) {
		ArrayList<Integer> pastDaysList = new ArrayList<>();
		for (int i = intervals-1; i >= 0; i--) {
			pastDaysList.add(Integer.parseInt(getPastDate(i)));
		}
		return pastDaysList;
	}

	// 获取过去N天的时间
	public static String getPastDate(int past) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
		Date today = calendar.getTime();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String result = format.format(today);
		return result;
	}
}
